package com.jeff.tianti.cms.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Entity
@Table(name = "media")//媒体供需表
@JsonIgnoreProperties(value = {"columnInfo", "area", "hibernateLazyInitializer", "handler", "fieldHandler"})
public class Media extends BaseEntity {
    private static final long serialVersionUID = -1738960604763689319L;

    @Column(name = "uid")
    private String uid;//用户主键
    private String mediaType;//媒体类型cms_column_info主键id关联
    private ColumnInfo columnInfo;
    @Column(name = "title")//标题
    private String title;
    private String mediaTypeName;//媒体类型名称
    @Column(name = "banner")
    private String banner;//轮播图url中间,隔开
    private List<String> banners;//轮播图字符串转集合
    @Column(name = "file")
    private String file;//图片file路径  中间,号隔开
    private String areaId;//area表主键
    private Area area;
    @Column(name = "address")
    private String address;//详细地址（省市区+详细地址）
    @Column(name = "company_name")
    private String companyName;//会员单位
    @Column(name = "phone")
    private String phone;//联系号码
    @Column(name = "content")
    private String content;//媒体介绍
    private String dateString;//时间字符串

    public String getDateString() {
        return DateUtils.dateToString(createDate);
    }

    public void setDateString(String dateString) {
        this.dateString = dateString;
    }

    @Transient
    public List<String> getBanners() {
        List<String> list;
        if (StringUtils.isNotBlank(banner)) {
            String[] split = banner.split(",");
            list = Arrays.asList(split);
            return list;
        } else {
            list = banners;
        }
        return list == null ? new ArrayList<>() : list;
    }

    @Transient
    public String getMediaTypeName() {
        if (columnInfo == null) {
            return "";
        } else {
            return columnInfo.getName();
        }
    }

    public void setMediaTypeName(String mediaTypeName) {
        this.mediaTypeName = mediaTypeName;
    }

    public void setBanners(List<String> banners) {
        this.banners = banners;
    }

    @ManyToOne(fetch = FetchType.LAZY, optional = false)//懒加载，不确保关联id能查询得到数据
    @JoinColumn(name = "media_type", updatable = false, insertable = false)
    public ColumnInfo getColumnInfo() {
        return columnInfo;
    }

    public void setColumnInfo(ColumnInfo columnInfo) {
        this.columnInfo = columnInfo;
    }

    @Column(name = "media_type")
    public String getMediaType() {
        return mediaType;
    }

    public void setMediaType(String mediaType) {
        this.mediaType = mediaType;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getBanner() {
        return banner;
    }

    public void setBanner(String banner) {
        this.banner = banner;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name = "area_id")
    public String getAreaId() {
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
    }

    @ManyToOne(fetch = FetchType.LAZY, optional = false)//懒加载，不确保关联id能查询得到数据
    @JoinColumn(name = "area_id", updatable = false, insertable = false)
    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
